package com.ybkj.daijia.server.event.listener;

import com.alibaba.fastjson.JSONObject;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.event.model.MemberRechargeEvent;
import com.ybkj.daijia.server.mc.Enterprise;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.model.SmsTemplet.SmsType;
import com.ybkj.daijia.server.reactor.ReactorVo;
import com.ybkj.daijia.server.service.ActivityService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.CouponService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.MemberAccountService;
import com.ybkj.daijia.server.service.MemberPaymentsRecordService;
import com.ybkj.daijia.server.service.MemberRechargeRecordService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.ShareRecordService;
import com.ybkj.daijia.server.service.SmsService;
import com.ybkj.daijia.server.service.SmsTempletService;
import java.math.BigDecimal;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class MemberRechargeEventListener implements ApplicationListener<MemberRechargeEvent> {

    private static final Logger logger = LoggerFactory.getLogger(MemberRechargeEventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private CouponService couponService;

    @Autowired
    private ShareRecordService shareRecordService;

    @Autowired
    private MemberRechargeRecordService memberRechargeRecordService;

    @Autowired
    private MemberPaymentsRecordService memberPaymentsRecordService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private MemberAccountService memberAccountService;

    @Autowired
    private SmsService smsService;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @Override
    @Async
    public void onApplicationEvent(MemberRechargeEvent event) {
        logger.debug("member Recharge event:{}", "");

        ReactorVo vo = (ReactorVo) event.getSource();
        Passenger passenger = memberService.findOnePassenger(vo.getPassenger().getId());
        BigDecimal money = vo.getMoney();
        String type = vo.getType();

        String phone = passenger.getPhone();

        Company maincompany = companyService.getMain();
        Company memberCompany = null;

        if (passenger.getType().equals(Passenger.TYPE_PASSENGER)) {
            //发送短信
            try {
                SmsTemplet templet = smsService
                    .findByAlias(passenger.getCompanyId(), SmsType.recharge);
                memberCompany = companyService.findOne(passenger.getCompanyId());

                if (null != templet && StringUtils.isNotBlank(templet.getContent()) && StringUtils
                    .isNotBlank(memberCompany.getSignName())) {
                    //smsService.sendSms(phone, SmsType.memberRechargeRecord.toString(), "{\"存入金额\":\""+money+"\",\"余额\":\""+passenger.getDrawMoney().add(passenger.getNodrawMoney())+"\",\"电话\":\""+maincompany.getTelephone()+"\"}", memberCompany.getId(), 0, memberCompany.getSignName());
                    //短信发送（改）
                    JSONObject smsParam = new JSONObject();
                    smsParam.put("money", money);
                    smsParam
                        .put("balance", passenger.getDrawMoney().add(passenger.getNodrawMoney()));
                    smsParam.put("tel", maincompany.getTelephone());
                    smsService.sendSms(phone, templet.getTemplateId(), smsParam.toJSONString(),
                        memberCompany.getId(), 0, memberCompany.getSignName());

                }
            } catch (Exception e) {
                logger.error("发送短信发生异常：{}", e.getMessage());
            }
        } else if (passenger.getType().equals(Passenger.TYPE_ENTERPRISE)) {
            //发送短信
            try {
                Enterprise enterprise = memberService
                    .findOneEnterprise(passenger.getEnterprise_id());
                SmsTemplet templet = smsService
                    .findByAlias(enterprise.getCompanyId(), SmsType.recharge);
                memberCompany = companyService.findOne(enterprise.getCompanyId());

                if (null != templet && StringUtils.isNotBlank(templet.getContent()) && StringUtils
                    .isNotBlank(memberCompany.getSignName())) {
                    //smsService.sendSms(phone, SmsType.memberRechargeRecord.toString(), "{\"存入金额\":\""+money+"\",\"余额\":\""+enterprise.getDrawMoney().add(enterprise.getNodrawMoney())+"\",\"电话\":\""+maincompany.getTelephone()+"\"}", memberCompany.getId(), 0, memberCompany.getSignName());
                    //短信发送（改）
                    JSONObject smsParam = new JSONObject();
                    smsParam.put("money", money);
                    smsParam
                        .put("balance", passenger.getDrawMoney().add(passenger.getNodrawMoney()));
                    smsParam.put("tel", maincompany.getTelephone());
                    smsService.sendSms(phone, templet.getTemplateId(), smsParam.toJSONString(),
                        memberCompany.getId(), 0, memberCompany.getSignName());
                }
            } catch (Exception e) {
                logger.error("发送短信发生异常：{}", e.getMessage());
            }
        }

        if (null != memberCompany && StringUtils.isNotBlank(maincompany.getMobilePhone())) {
            try {

                SmsTemplet rechargeTemplet = smsService
                    .findByAlias(passenger.getCompanyId(), SmsType.recharge);

                String typeStr = "";
                if ("admin".equals(type)) {
                    typeStr = "后台充值";
                } else if ("alipay".equals(type)) {
                    typeStr = "支付宝充值";
                } else if ("weixin".equals(type)) {
                    typeStr = "微信充值";
                } else if ("unionpay".equals(type)) {
                    typeStr = "银联充值";
                }
                if (null != rechargeTemplet && StringUtils.isNotBlank(rechargeTemplet.getContent())
                    && StringUtils.isNotBlank(memberCompany.getSignName())) {
                    //smsService.sendSms(maincompany.getMobilePhone(), "recharge", "{\"名称\":\"客户【"+passenger.getName()+"】\",\"充值金额\":\""+money+"元\",\"充值方式\":\""+typeStr+"\",\"电话\":\""+passenger.getPhone()+"\"}", memberCompany.getId(), 0, memberCompany.getSignName());
                    //短信发送（改）
                    JSONObject smsParam = new JSONObject();
                    smsParam.put("money", money);
                    smsParam
                        .put("balance", passenger.getDrawMoney().add(passenger.getNodrawMoney()));
                    smsParam.put("tel", maincompany.getTelephone());
                    smsService
                        .sendSms(maincompany.getMobilePhone(), rechargeTemplet.getTemplateId(),
                            smsParam.toJSONString(), memberCompany.getId(), 0,
                            memberCompany.getSignName());
                }

            } catch (Exception e) {
                logger.error("发送短信发生异常：{}", e.getMessage());
            }
        }


    }

}
